<?php
/**
 * config helper
 *
 * @author tom.wang<tom.wang.sz@qq.com>
 */
class ConfigUtil {
	private static $configs;
	
	public static function initCfg($cfg) {
		$configs = array(
			'app' => require FRAMEWORK_QING_ROOT . 'Conf/app.php',
			'db' => require FRAMEWORK_QING_ROOT . 'Conf/db.php',
			'filter' => require FRAMEWORK_QING_ROOT . 'Conf/filter.php',
		);
		
		if(!is_array($cfg)) $cfg = array();
		
		if(isset($cfg['app'])) {
			$configs['app'] = array_merge($configs['app'], $cfg['app']);
		}
		
		if(isset($cfg['db'])) {
			$configs['db'] = array_merge($configs['db'], $cfg['db']);
		}
		
		if(isset($cfg['filter'])) {
			$configs['filter'] = array_merge($configs['filter'], $cfg['filter']);
		}
		
		self::$configs = $configs;
	}
	
	public static function getAppCfg($key) {
		return self::getCfg('app', $key);
	}
	
	public static function getDbCfg($key) {
		return self::getCfg('db', $key);
	}
	
	public static function getFilterCfg($key) {
		return self::getCfg('filter', $key);
	}
	
	public static function getAllCfg() {
		return self::$configs;
	}
	
	private static function getCfg($type, $key) {
		$ret = null;
		
		if(isset(self::$configs[$type])) {
			if(null === $key) $ret = self::$configs[$type];
			elseif(isset(self::$configs[$type][$key])) $ret = self::$configs[$type][$key];
		}
		
		return $ret;
	}
}